package org.maxgroundstation.groundstation.view;

import java.awt.FlowLayout;
import java.lang.reflect.Method;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicSliderUI;

import org.maxgroundstation.groundstation.GroundStation;

/**
 * A panel for the master governor control.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class MasterGovernorPanel extends JPanel {

    // Data members
    private GroundStation gs;
    
    public MasterGovernorPanel(GroundStation gs) {
        super();
        
        // Initialize data members.
        this.gs = gs;
        
        // Set the layout.
        setLayout(new FlowLayout(FlowLayout.CENTER));
        
        // Set the border.
        setBorder(new TitledBorder("Master Governor"));

        // Create slider bar panel.
        JPanel sliderBarPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
        add(sliderBarPanel);
        
        // Create master governor slider bar.
        float currentMasterGovernor = gs.getInputPoller().getMasterGovernor();
        JSlider masterGovernorSlider = new JSlider(0, 10, (int) (currentMasterGovernor * 10F));
        masterGovernorSlider.setMajorTickSpacing(1);
        masterGovernorSlider.setPaintTicks(true);
        masterGovernorSlider.setPaintLabels(true);
        masterGovernorSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider slider = (JSlider) e.getSource();
                float value = (float) slider.getValue() / 10F;
                getGroundStation().getInputPoller().setMasterGovernor(value);
            }
        });
        masterGovernorSlider.setToolTipText("Set the governor level for the rover's drive power.");
        removeSliderKeyboardBinding(masterGovernorSlider);
        sliderBarPanel.add(masterGovernorSlider);
    }
    
    /**
     * Gets the ground station.
     * @return the ground station.
     */
    private GroundStation getGroundStation() {
        return gs;
    }
    
    /**
     * Removes keyboard bindings from a slider component.
     * @param slider the slider component.
     */
    private void removeSliderKeyboardBinding(JSlider slider) {
        try {
            Method uninstallKeyboardActions = BasicSliderUI.class.getDeclaredMethod("uninstallKeyboardActions",
                    new Class[] { JSlider.class });
            uninstallKeyboardActions.setAccessible(true);
            uninstallKeyboardActions.invoke(slider.getUI(), slider);
        }
        catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}